package com.tdam2011.grupo02.ui;

import com.tdam2011.grupo02.R;
import com.tdam2011.grupo02.compatibility.ActionBarFragmentActivity;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends ActionBarFragmentActivity implements
		OnClickListener {

	Intent intent;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ImageButton contactos = (ImageButton) findViewById(R.id.imgbtn_contactos);
		ImageButton msgweb = (ImageButton) findViewById(R.id.imgbtn_msgweb);
		ImageButton mail = (ImageButton) findViewById(R.id.imgbtn_mail);
		ImageButton log = (ImageButton) findViewById(R.id.imgbtn_logs);
		ImageButton conectividadLog = (ImageButton) findViewById(R.id.imgbtn_conectividad_log);
		ImageButton perfil = (ImageButton) findViewById(R.id.imgbtn_perfil);	
		
		contactos.setOnClickListener(this);
		msgweb.setOnClickListener(this);
		mail.setOnClickListener(this);
		log.setOnClickListener(this);
		conectividadLog.setOnClickListener(this);
		perfil.setOnClickListener(this);		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imgbtn_contactos:
			intent = new Intent().setClass(this, ContactosListActivity.class);
			break;

		case R.id.imgbtn_msgweb:
			intent = new Intent().setClass(this, MensajeriaWebFragmentActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			break;

		case R.id.imgbtn_mail:
			ConnectivityManager connectivity;
			NetworkInfo wifiInfo,
			mobileInfo;

			connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			wifiInfo = connectivity
					.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			mobileInfo = connectivity
					.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

			if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
				intent = new Intent()
						.setAction(android.content.Intent.ACTION_SEND);
				intent.setType("plain/text");
			} else {
				intent = null;
				Toast.makeText(this,
						getString(R.string.menuMainNoConnectionForMail),
						Toast.LENGTH_SHORT).show();
			}
			break;

		case R.id.imgbtn_logs:
			intent = new Intent().setClass(this,
					HistorialLogFragmentActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			break;

		case R.id.imgbtn_conectividad_log:
			intent = new Intent().setClass(this, ConnectivityLogListActivity.class);
			break;

		case R.id.imgbtn_perfil:
			intent = new Intent().setClass(this, PerfilActivity.class);
			break;
		}

		if (intent != null)
			try{
				startActivity(intent);
			}
			catch(ActivityNotFoundException e){};
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu_main, menu);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = null;
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();			
			break;

		case R.id.mainConfiguracion:
			break;

		case R.id.menu_share:
			shareContactDroid();
			return true;			
		}

		if (intent != null)
			startActivity(intent);

		return super.onOptionsItemSelected(item);
	}

	private void shareContactDroid() {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");

		intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.shareAppSubject));
		intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.shareAppText)+"\n"+getString(R.string.shareAppLink));

		startActivity(Intent.createChooser(intent, getString(R.string.shareAppTitle)));
	}
}